from bs4 import BeautifulSoup
from bs4 import NavigableString


'''
body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > img
body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.caption > h4:nth-child(2) > a
body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.caption > h4.pull-right
body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.caption > p
body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.ratings > p.pull-right
body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(2) > div > div.ratings > p:nth-child(2) > span:nth-child(1)
'''

def count_start_num(tag):
    return (isinstance(tag.next_element, NavigableString)
          and isinstance(tag.previous_element, NavigableString))

path = './1_2_homework_required/index.html'
with open(path) as f:
    soup = BeautifulSoup(f.read(),'lxml')
    # print(soup)
    imgs = soup.select("body > div > div > div.col-md-9 > div > div > div > img")
    # print(imgs)
    titles = soup.select("body > div > div > div.col-md-9 > div > div > div > div.caption > h4 > a")
    # print(titles)
    prices = soup.select("body > div > div > div.col-md-9 > div > div > div > div.caption > h4.pull-right")
    # print(prices)
    descs = soup.select("body > div > div > div.col-md-9 > div > div > div > div.caption > p")
    # print(descs)
    votes = soup.select("body > div > div > div.col-md-9 > div > div > div > div.ratings > p.pull-right")
    # print(votes)
    stars = soup.select("body > div > div > div.col-md-9 > div > div > div > div.ratings > p:nth-of-type(2)")
    # print(starts)

data = []
for title, img, price, desc, vote, star in zip(titles, imgs, prices, descs, votes, stars):
    info = {
        "title": title.get_text(),
        "img": img.get("src"),
        "price": price.get_text(),
        "desc": desc.get_text(),
        "vote": vote.get_text(),
        "star": len(star.find_all('span', 'glyphicon glyphicon-star'))
    }
    data.append(info)

for i in data:
    if i['star'] > 4:
        print(i['title'], i['star'])